﻿using ServiceCore.DbContext;
using ServiceCore.Entity.Core;
using ServiceCore.Entity.Model;
using ServiceCore.Entity.Param.User;
using ServiceCore.Utility.Extension;
using SqlSugar;

namespace ServiceCore.Service
{
    /// <summary>
    /// 用户服务
    /// </summary>
    public partial class UserInfoService : SqlsugarServices<UserInfo>
    {
        /// <summary>
        /// 分页 
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public PageParam<UserInfo, List<UserInfo>> User_PageList(PageParam<UserInfo, List<UserInfo>> arg)
        {
            var recordCount = 0;
            arg.Data = this.Context.Queryable<UserInfo>()
                .WhereIF(string.IsNullOrEmpty(arg.Keywords) == false, x => x.UserName.Contains(arg.Keywords) || x.Account.Contains(arg.Keywords))
                .WhereIF(string.IsNullOrEmpty(arg.BeginTime) == false, x => x.CreateTime >= DateTime.Parse(arg.BeginTime))
                .WhereIF(string.IsNullOrEmpty(arg.EndTime) == false, x => x.CreateTime < DateTime.Parse(arg.EndTime).AddDays(1))
                .WhereIF(arg.Param != null && arg.Param.IsUsed.HasValue, x => x.IsUsed == arg.Param.IsUsed)
                .OrderByDescending(x => x.CreateTime)
                .ToPageList(arg.PageIndex, arg.PageSize, ref recordCount);

            arg.RecordTotal = recordCount;
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<UserInfo, UserInfo> User_Exists(BaseParam<UserInfo, UserInfo> arg, bool? isAdd = null)
        {
            arg.Data = this.Context.Queryable<UserInfo>()
                .WhereIF(arg.Id > 0, x => x.Id == arg.Id)
                .WhereIF(arg.Param != null && string.IsNullOrEmpty(arg.Param.Account) == false, x => x.Account == arg.Param.Account)
                .WhereIF(isAdd.HasValue && isAdd == false, x => x.Id != arg.Param.Id)
                .First();

            return arg;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public BaseParam<UserInfo, UserInfo> User_Login(BaseParam<UserInfo, UserInfo> arg)
        {
            arg = User_Login_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            arg = User_Exists(arg);
            if (arg.Data.IsNull())
                return new BaseParam<UserInfo, UserInfo>("账号或密码错误");            
            if (arg.Data.Pwd != arg.Param.Pwd.EncodeDES())
                return new BaseParam<UserInfo, UserInfo>("账号或密码错误");
            if (arg.Data.IsUsed == 0)
                return new BaseParam<UserInfo, UserInfo>("用户已停用");

            User_LastLoginTime(arg);
            arg.Param = null;

            return arg;
        }
        /// <summary>
        /// 最后登录时间
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<UserInfo, UserInfo> User_LastLoginTime(BaseParam<UserInfo, UserInfo> arg)
        {
            this.Context.Updateable<UserInfo>()
                .SetColumns(x => new UserInfo() { LastLoginTime = DateTime.Now })
                .Where(x => x.Id == arg.Data.Id)
                .ExecuteCommand();

            return arg;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<UserInfo, long> User_Add(BaseParam<UserInfo, long> arg)
        {
            arg = User_Add_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            arg.Data = InsertReturnBigIdentity(arg.Param);
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<UserInfo, long> User_Edit(BaseParam<UserInfo, long> arg)
        {
            arg = User_Edit_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            arg.Data = this.Context.Updateable<UserInfo>()
                .SetColumns(x => new UserInfo() { UserName = arg.Param.UserName, Pwd = arg.Param.Pwd })
                .Where(x => x.Id == arg.Param.Id)
                .ExecuteCommand();
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 启用禁用
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<UserInfo, long> User_Used(BaseParam<UserInfo, long> arg)
        {
            arg = User_IsUsed_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            arg.Data = this.Context.Updateable<UserInfo>()
                .SetColumns(x => new UserInfo() { IsUsed = arg.Param.IsUsed })
                .Where(x => x.Id == arg.Param.Id)
                .ExecuteCommand();
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<UserChangePwdParam, long> User_ChangePwd(BaseParam<UserChangePwdParam, long> arg)
        {
            arg = User_ChangePwd_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;

            arg.Data = this.Context.Updateable<UserInfo>()
                .SetColumns(x => new UserInfo() { Pwd = arg.Param.NewPwd })
                .Where(x => x.Account == arg.Param.Account)
                .ExecuteCommand();
            arg.Param = null;

            return arg;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public BaseParam<UserInfo, long> User_Delete(BaseParam<UserInfo, long> arg)
        {
            arg = User_Del_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;
            arg.Data = this.Context.Deleteable<UserInfo>().Where(x => x.Id == arg.Id).ExecuteCommand();
            arg.Param = null;

            return arg;
        }
    }
}
